var data;
var that = this;
getData();
function getData(){
    data={"category":getUrlParam("category")==null?1:getUrlParam("category")
        ,"key_id":getUrlParam("key_id")
        ,"name":getUrlParam("name")
        ,"update_time":getUrlParam("update_time")==null?0:getUrlParam("update_time")};
}
layui.use('table', function(){
    var table = layui.table;
    var notify = layui.notify;
    table.on('tool(test)', function(obj){
        if(obj.event === 'edit'){
            layer.confirm('真的要还原默认吗？', function(index){
                /*
                普通：info
                警告：warning
                成功：success
                错误：error
                加载：loading
                关闭：destroyAll
                 */
                $.ajax({
                    url: cxt + "/admin/img/examine/setImg",
                    data:{KEY_ID:obj.data.key_ID,category:data.category},
                    type: "post",
                    dataType: "json",
                    success: function (data) {
                        if(data.state==1){
                            notify.success("设置成功！！！");
                        }else if(data.state==0){
                            notify.warning("设置失败！！！");
                        }else {
                            notify.error("网络异常！！！");
                        }
                    },
                    error: function () {
                        notify.error("网络异常！！！");
                    }
                })
                layer.close(index);
            });
        }
    });
    table.render({
        elem: '#test'
        ,url:cxt+'/admin/img/examine/getImg'
        ,where: data
        ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
        ,defaultToolbar: []
        ,title: '用户数据表'
        ,cols: [[
            {field:'key_ID', title:data.category==1?'学号':'职工号', minWidth: 200, align:'center'}
            ,{field:'name', title:'用户名', minWidth:120, align:'center'}
            ,{field:'organization_NAME', title:data.category==1?'班级名称':"单位名称", minWidth:220, align:'center'}
            ,{field:'icon', title:'头像', width:85, templet:"#img"}
            ,{field:'icon', title:'更新时间', minWidth:200, templet:function (data) {
                var d=data.icon;
                if(d.substring(0,1)==1 || d=='defaulticon.png'){
                    d=d.substring(1,d.length);
                }
                if(d.length<15){
                    return data.UPDATE_TIME;
                }
                return d.substring(0,4)+"-"+d.substring(4,6)+"-"+d.substring(6,8)+" "+d.substring(8,10)+":"+d.substring(10,12)+":"+d.substring(12,14);
                }, align:'center'}
            ,{field:'UPDATE_TIME', title:'最近登录时间', minWidth: 200, align:'center'}
            ,{title:'操作', toolbar: '#barDemo', width: 100, align:'center'}
        ]]
        //,height: 'full-200'
        ,limit: 10
        ,page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
            layout: ['count', 'prev', 'page', 'next', 'skip','limit'] //自定义分页布局
            //,curr: 5 //设定初始在第 5 页
            ,groups: 3 //只显示 1 个连续页码
            // ,first: false //不显示首页
            // ,last: false //不显示尾页
            ,prev: "上一页"
            ,next: "下一页"
            ,align: 'center'
        }
        ,limits: [5,10,15,20,25,30,35,40,45,50]
        ,request: {
            pageName: 'page_num' //页码的参数名称，默认：page
            ,limitName: 'page_size' //每页数据量的参数名，默认：limit

        }
        ,response: {
            statusCode: 1 //重新规定成功的状态码为 200，table 组件默认为 0
        }
        ,parseData: function(res){ //将原始数据解析成 table 组件所规定的数据
            return {
                "code": res.state, //解析接口状态
                "count": data.category==1?res.student.total:res.employee.total, //解析数据长度
                "data": data.category==1?res.student.list: res.employee.list//解析数据列表
            };
        }
    });
    $("#key_id").val(data.key_id)
    $("#name").val(data.name)
    $("#update_time").val(data.update_time)
    $("#category").val(data.category)
    $("#search").on('click',function () {
        var key_id=$("#key_id").val();
        var name=$("#name").val();
        var update_time=$("#update_time").val();
        var category=$("#category").val();
        data={"category":category,"key_id":key_id,"name":name,"update_time":update_time};
        window.location.href="?category="+category+"&key_id="+key_id+"&name="+name+"&update_time="+update_time
    })
});
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)","i"); //构造一个含有目标参数的正则表达式对象
    var r = decodeURI(window.location.search.substr(1)).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]); return null; //返回参数值
}
function img(t) {
    //页面层
    layer.open({
        type: 1,
        title: '查看大图',
        skin: 'layui-layer-rim', //加上边框
        area: ['451px', '451px'], //宽高
        shadeClose: true, //开启遮罩关闭
        end: function (index, layero) {
            return false;
        },
        content: '<div style="text-align:center"><img width="400" height="400" src="' + $(t).attr('src') + '" /></div>'
    });
}
